package com.dddd.blog.blogsp.controller;


import com.dddd.blog.blogcommon.constant.ResultCode;
import com.dddd.blog.blogcommon.controller.BaseController;
import com.dddd.blog.blogcommon.pojo.Result;
import com.dddd.blog.blogcommon.utils.ResultUtils;
import com.dddd.blog.blogsp.feign.ForPostFeignClient;
import com.dddd.blog.blogsp.feign.ForUserFeignClient;
import com.dddd.blog.blogsp.service.AuthorService;
import com.dddd.blog.blogsp.service.BlogContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 喵星球的大橘
 */
@RestController
@RequestMapping("/spider")
@Api(tags = {"爬虫管理接口"})
public class SpiderAuthorController extends BaseController {

    @Autowired
    private AuthorService authorService;


    @Autowired
    private BlogContentService blogContentService;

    @Autowired
    private ForUserFeignClient forUserFeignClient;


    @Autowired
    private ForPostFeignClient forPostFeignClient;

    /**
     *
     * @return
     */
    @GetMapping("/loadNames")
    @ApiOperation(value = "从spider库中加载用户昵称列表")
    public Result<List<String>> loadAuthorList()
    {

        List<String> authorList = authorService.getAuthorNameList();
        return ResultUtils.success(authorList);

    }


    /**
     *
     * @return
     */
    @GetMapping("/loadAuthors")
    @ApiOperation(value = "从Spider服务中获取用户map属性列表")
    public Result loadAuthorList2(){

        List<Map<String, Object>> authorList = authorService.getAuthorList();

        System.err.println(authorList);

        if(authorList != null){
            return ResultUtils.success(authorList);
        }else{
            return ResultUtils.error(ResultCode.NOT_FOUND);
        }
    }


    /**
     *
     * @return
     */
    @PostMapping("/loadAuthor/page")
    @ApiOperation(value = "从Spider服务中分页获取用户map属性列表")
    public Result loadAuthorList2(@ApiParam("currentPage") Long currentPage,
                                  @ApiParam("pageSize") Long pageSize){

        this.getLogger().info("currentPage: " + currentPage);

        List<Map<String, Object>> listPage = authorService.getAuthorListPage(currentPage, pageSize);
        if(listPage == null)
        {
            return ResultUtils.error(ResultCode.FAIL);
        }

        return ResultUtils.success(listPage);
    }



    @GetMapping("/loadBlog")
    @ApiOperation(value = "从spider库中加载博客数据")
    public Result loadBlogList(@ApiParam("currentPage") long currentPage,
                               @ApiParam("pageSize") long pageSize)
    {
        return null;
    }



    @GetMapping("/loadPosts")
    @ApiOperation(value = "从spider数据库中加载博客到post数据库")
    public Result load(){

        // 从数据库获取文章源
        List<Map<String, Object>> postList = blogContentService.getPostList();

        blogContentService.addPostByMap(postList.get(0));

        try{
            postList.stream().forEach(item ->blogContentService.addPostByMap(item));
            return ResultUtils.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultUtils.error(ResultCode.INTERNAL_ERROR);
    }

}
